
package com.laikan.library.components.fragments;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.laikan.library.R;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.helpers.BasicEvent;
import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * A placeholder fragment containing a simple view.
 */
public abstract class BasicFragment extends Fragment implements View.OnClickListener {

    protected String TAG;
    protected LinearLayout mBodyView;
    protected Context mContext;
    protected Activity mFragmentActivity;
    protected boolean isEventBusOpen = false;
    protected boolean isLocalBroadcastReceiverOpen = false;
    protected BroadcastReceiver local_fragment_receiver;
    private LayoutInflater mLayoutInflater;
    private boolean isRestart = true;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mFragmentActivity = getActivity();
        TAG = this.getClass().getSimpleName();
//        checkPermission();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.base_fragment, container, false);
        mLayoutInflater = inflater;
        mBodyView = (LinearLayout)view.findViewById(R.id.base_fragment_body);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        KLog.i(TAG + "onActivityCreated");

        assignViews();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isRestart) {
            init();
            isRestart = false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(mContext);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(mContext);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isEventBusOpen) {

            EventBus.getDefault().unregister(this);
        }

        if (isLocalBroadcastReceiverOpen) {

            LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(local_fragment_receiver);
        }
        isRestart = true;
    }

    protected void checkPermission() {}


    private void init() {
        initData();
        setListener();
    }

    protected void setFragmentContentView(int viewID) {

        if (mLayoutInflater == null) {
            mLayoutInflater = LayoutInflater.from(getActivity());
        }
        View bodyView = mLayoutInflater.inflate(viewID, null);

        if (mBodyView != null) {
            mBodyView.removeAllViews();
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mBodyView.addView(bodyView, layoutParams);
    }

    protected void goToActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        startActivity(intent);
    }

    protected void sendLocalBroadCastReceiver(Intent intent) {
        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
    }

    /**
     * 加载布局和初始化View组件
     */
    protected abstract void assignViews();

    /**
     * 初始化View组件数据
     */
    protected abstract void initData();

    /**
     * 为组件注册监听?
     */
    protected abstract void setListener();

    /**
     * 处理onClick事件
     */
    protected abstract void onClickEvent(View view);

    @Override
    public void onClick(View view) {
        onClickEvent(view);
    }

    public void openBroadcastReceiver(String action) {

        local_fragment_receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                parseLocalBroadCast(intent);
            }
        };
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(local_fragment_receiver, new IntentFilter(action));
        this.isLocalBroadcastReceiverOpen = true;
    }

    protected void parseLocalBroadCast(Intent intent) {
    }

    ;

    public void openEventBus() {
        EventBus.getDefault().register(this);
        this.isEventBusOpen = true;
    }

    public void postEvent(BasicEvent event) {
        EventBus.getDefault().post(event);
    }

    /**
     * EventBus 的解析器
     *
     * @param event
     */
    protected void parseEvent(BasicEvent event) {
    }

    ;

    protected void parseMainThreadEvent(BasicEvent event) {
    }

    ;

    protected void parseBackgroundEvent(BasicEvent event) {
    }

    ;

    protected void parseAsyncEvent(BasicEvent event) {
    }

    ;

    @Subscribe
    public void onEvent(BasicEvent event) {

        parseEvent(event);
    }

    @Subscribe
    public void onEventMainThread(BasicEvent event) {

        parseMainThreadEvent(event);
    }

    @Subscribe
    public void onEventBackground(BasicEvent event) {

        parseBackgroundEvent(event);
    }

    @Subscribe
    public void onEventAsync(BasicEvent event) {

        parseAsyncEvent(event);
    }

    /**
     * 打开加载框
     */
    public void showDialog(){
        Activity activity = getActivity();
        if(activity instanceof BasicActivity){
            ((BasicActivity)activity).showDialog();
        }
    }

    /**
     * 关闭加载框
     */
    public void dismissDialog(){
        Activity activity = getActivity();
        if(activity instanceof BasicActivity){
            ((BasicActivity)activity).dismissDialog();
        }
    }
}

